package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.bookstore.domain.Brand;
import com.chidopi.market.bookstore.domain.ContextProvider;

public class BrandServiceImpl implements IBaseService<Brand> {
	
	private static Logger logger = LoggerFactory.getLogger(BrandServiceImpl.class);
	
	@Autowired
	@Qualifier("brandDao")
	private IBaseDao<Brand> baseDao;
	
	@Autowired
	@Qualifier("contextProviderDao")
	private IBaseDao<ContextProvider> cpDao;
	
	@Override
	public int insert(Brand t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(Brand t) throws Exception {
		return this.baseDao.update(t);
	}

	@Override
	public int delete(Brand t) throws Exception {
		Integer id = t.getId();
		List<ContextProvider> cps = this.cpDao.find("brand_id = ? ", id);
		if (cps.size() > 0) {
			logger.debug("delete [" + id + "] failure: contxtProvider.size()=" + cps.size());
			throw new CdfException("error.brand.delete.contextprovider");
		}
		return this.baseDao.delete(t);
	}

	@Override
	public List<Brand> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public Brand findById(Object id) throws Exception {
		return this.baseDao.findById(id);
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

}
